<?php
	//开启SESSION
	session_start();

    // 	判断用户是否登入
	// if(empty($_SESSION['name'])){
	// 	header("refresh:0.1,url=./login.php");
	// 	die();
    // }

// 禁用报错显示
// error_reporting(0);
// ini_set('display_errors', 0);


	//连接数据库
	include 'php/conn.php';


	
	//SQL语句 查询stuinfo表所有数据
    $sql = "SELECT * FROM stuinfo";
	
	$result = mysqli_query($link,$sql);
	
	$arr = mysqli_fetch_all($result,MYSQLI_ASSOC);
	
	$count = count($arr);
	
	$sex = '';
	for($i = 0; $i < count($arr); $i++){
		$sex .=  $arr[$i]['sex'];
	}
	$man = substr_count($sex,'男');
	$woman = substr_count($sex,'女');
	
	//页码总数
	$pages = ceil($count/10);
	
	$page = isset($_GET['page'])?$_GET['page']:1;
	$starRow = ($page-1)*10;



//SQL语句 查询stuinfo表数据

if (isset($_POST['ok'])) {
    $id = $_POST['id'];
    $name = $_POST['name'];
    $sex = $_POST['sex'];
    $age = $_POST['age'];
    $uuid = $_POST['uuid'];
    $clas = $_POST['clas'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];

    if (!empty($email)) {
        $sql2 = "SELECT * FROM stuinfo WHERE email = '$email'";
    } elseif (!empty($clas)) {
        $sql2 = "SELECT * FROM stuinfo WHERE clas = '$clas'";
    } elseif (!empty($uuid)) {
        $sql2 = "SELECT * FROM stuinfo WHERE uuid = '$uuid'";
    } elseif (!empty($id)) {
        $sql2 = "SELECT * FROM stuinfo WHERE id = '$id'";
    } elseif (!empty($phone)) {
        $sql2 = "SELECT * FROM stuinfo WHERE phone_number = '$phone'";
    } elseif (!empty($age)) {
        $sql2 = "SELECT * FROM stuinfo WHERE age = '$age'";
    } elseif (!empty($sex)) {
        $sql2 = "SELECT * FROM stuinfo WHERE sex = '$sex'";
    } elseif (!empty($name)) {
        $sql2 = "SELECT * FROM stuinfo WHERE name = '$name'";
    }

}else{
    // 排序学生
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_POST['id_on'])){
            $sql2 = "SELECT * FROM stuinfo ORDER BY id ASC ";
        } 
        if(isset($_POST['id_up'])){
            $sql2 = "SELECT * FROM stuinfo ORDER BY id DESC ";
        }
        if(isset($_POST['name_on'])){
            $sql2 = "SELECT * FROM stuinfo ORDER BY name ASC ";
        } 
        if(isset($_POST['name_up'])){
            $sql2 = "SELECT * FROM stuinfo ORDER BY name DESC ";
        }
        if(isset($_POST['age_on'])){
            $sql2 = "SELECT * FROM stuinfo ORDER BY age ASC ";
        } 
        if(isset($_POST['age_up'])){
            $sql2 = "SELECT * FROM stuinfo ORDER BY age DESC ";
        }
        if(isset($_POST['phone_on'])){
            $sql2 = "SELECT * FROM stuinfo ORDER BY phone_number ASC ";
        } 
        if(isset($_POST['phone_up'])){
            $sql2 = "SELECT * FROM stuinfo ORDER BY phone_number DESC ";
        }
        if(isset($_POST['sex_on'])){
            $sql2 = "SELECT * FROM stuinfo ORDER BY sex DESC ";
        } 
        if(isset($_POST['sex_up'])){
            $sql2 = "SELECT * FROM stuinfo ORDER BY sex ASC ";
        }
     }else{
        // 刷新网页
    // header('Location: index.php');
    $sql2 = "select  *  from  stuinfo  limit  ".$starRow.",  12;";

}
}


	$result2 = mysqli_query($link,$sql2);
	$arr2 = mysqli_fetch_all($result2,MYSQLI_ASSOC);
	
	
	//防止从url篡改大于总页码的参数报错
	if(isset($_GET['page'])){
		if($_GET['page'] > $pages){
			die();
		}
	}
	
	
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>学生信息管理平台</title>
    <link rel="stylesheet" href="layui1/css/layui.css" />
    <link rel="stylesheet" href="css/index.css" />


</head>

<body>
<div class="top_lion">
<span></span>
</div>

    <table class="layui-table" lay-skin="line" id="tableExcel">
        <caption style="text-align:center">
            <h2>学生信息管理系统</h2>

            <div class="btn_left">
                <a>
                    数据库已录入:<?php echo $count;?>人&nbsp;
                </a>
                <a>
                    男:<?php echo $man;?>人&nbsp;
                </a>
                <a>
                    女:<?php echo $woman;?>人&nbsp;
                </a>
            </div>

            <div class="btn_right">
                <a title="返回主页" class="home" href="index.php" onclick="show(this.getAttribute('title'),'index.php')">
                    <i class="layui-icon layui-icon-home"></i>
                </a>&nbsp;&nbsp;
                <a title="查询学生" class="search_btn" onclick="show(this.getAttribute('title'),'select.php')">
                    <i class="layui-icon layui-icon-search"></i>
                </a>&nbsp;&nbsp;
                <a title="排序学生" class="many_sort" onclick="show(this.getAttribute('title'),'sort.php')">
                    <i class="layui-icon layui-icon-list"></i>
                </a>&nbsp;&nbsp;
                <a title="添加学生" class="add_btn" onclick="show(this.getAttribute('title'),'add.php')">
                    <i class="layui-icon layui-icon-add-circle-fine"></i>
                </a>&nbsp;&nbsp;
                <a title="删除学生" class="many_del">
                    <i class="layui-icon layui-icon-delete"></i>
                </a>&nbsp;&nbsp;
                <a title="点击下载" class="download" id="export">
                    <i class="layui-icon layui-icon-download-circle"></i>
                </a>
            </div>
        </caption>
        <thead>
            <tr>
                <th width="20"></th>
                <th class="id"></th>
                <th>编号</th>
                <th>学号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>专业</th>
                <th>年龄</th>
                <th>手机号码</th>
                <th>身份号码</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <?php for($i = 0; $i < count($arr2); $i++){?>
            <tr>
                <td class="id"><?php echo $arr2[$i]['id'];?></td>
                <td>
                    <form class="layui-form" action="">
                        <input type="checkbox" class="checkbox" lay-skin="primary">
                    </form>
                </td>
                <td><?php echo $i+1;?></td>

                <td><?php echo $arr2[$i]['id'];?></td>
                <td><?php echo $arr2[$i]['name'];?></td>
                <td><?php echo $arr2[$i]['sex'];?></td>
                <td><?php echo $arr2[$i]['clas'];?></td>
                <td><?php echo $arr2[$i]['age'];?></td>
                <td><?php echo $arr2[$i]['phone_number'];?></td>
                <td><?php echo $arr2[$i]['uuid'];?></td>

                <td>
                    <a title="详情" href="javascript:;"
                        onclick="show('查看详情','info.php?id=<?php echo $arr2[$i]['id'];?>')"><i
                            class="layui-icon layui-icon-about" style="color: #1E9FFF;"></i></a>&nbsp;
                    <a title="修改" href="javascript:;"
                        onclick="show('修改信息','update.php?id=<?php echo $arr2[$i]['id'];?>')"><i
                            class="layui-icon layui-icon-edit" style="color: #1E9FFF;"></i></a>&nbsp;
                    <a title="删除" class="single_del" href="javascript:;"><i class="layui-icon layui-icon-delete"
                            style="color: red;"></i> </a>
                            
                </td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
    <div id="page" style="margin:0 auto">
        <ul class="pagination">
            <?php
				if($page == 1){
					echo '<li><span>&laquo;</span></li>';
				}else {
					echo "<li><a title='首页' href='./?page=1'>首页</a></li>";
					echo "<li><a title='上一页' href='./?page=".($page-1)."' disabled>&laquo;</a></li>";
				}
				
				if($page<=$pages){
					$start = 1;
					$end = 5;
				}
				if($page > $end-2){
					$start = $page - 2;
					$end = $page + 2;
				}
				if($page + 3 >= $pages){
					$start = $page - 2;
					$end = $pages;
				}				
				
				for($i = $start; $i <= $end; $i++){
					echo "<li>";
					if($i==$page){
						echo "<span class='active'>$i</span>";
					}else {
						echo "<a href='?page=$i'>$i</a>";
					}
				}
				
				if($page < $pages){
					echo "<li><a title='下一页' href='./?page=".($page+1)."'>&raquo;</a></li>";
					echo "<li><a title='尾页' href='./?page=".$pages."'>尾页</a></li>";
					
				}else {
					echo '<li><span>&raquo;</span></li>';
				}
			?>

        </ul>
    </div>

    <script src="layui1/layui.js"></script>
    <script src="js/eject.js"></script>
    <script src="js/index.js"></script>
</body>

</html>